// pages/member/member.js
import * as API_Order from '../../api/order'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    vipPrice: 0,
    list: [],
    isVip: 'N',
    endTime: ''
  },
  pay () {
    getApp().https.ajax({
      method: 'post',
      url: '/order/createVip',
      loading: true
    }).then(({ data }) => {
      let params = {
        orderId: data.id
      };
      API_Order.delayPay(params).then(res => {
        if (res.code == 200) {
          let payment = res.data;
          if (payment.status == "succeeded") {
            let expend = payment.expend;
            let pay_info = JSON.parse(expend.pay_info);
            wx.requestPayment({
              'timeStamp': pay_info.timeStamp,
              'nonceStr': pay_info.nonceStr,
              'package': pay_info.package,
              'signType': pay_info.signType,
              'paySign': pay_info.paySign,
              'success': function (res) {
                wx.showToast({
                  title: '支付成功',
                })
                getApp().globalData.isVip = 'Y'
                let member = wx.getStorageSync('member')
                member.isVip = 'Y'
                wx.setStorageSync('member', member)
                setTimeout(() => {
                  wx.navigateBack()
                }, 1000)
              }
            })
          }
        } else {
          wx.showToast({
            title: res.msg,
            icon: 'none'
          })
        }
      })
    })
  },
  getList () {
    getApp().https.ajax({
      method: 'post',
      url: '/member/myVipInfo',
      data: {
        pageNum: 1,
        pageSize: 6
      },
      loading: true
    }).then(({ data }) => {
      const today = new Date();
      const nextYear = new Date(today.getFullYear() + 1, today.getMonth(), today.getDate());
      let endTime = nextYear.getFullYear()+'.'+(nextYear.getMonth()+1)+'.'+nextYear.getDate()
      if (data.member.isVip === 'Y') {
        endTime = data.member.vipEndDate.replaceAll('-', '.')
      }
      this.setData({
        vipPrice: data.vipPrice,
        list: data.storeProductList.rows,
        isVip: data.member.isVip,
        endTime: endTime
      })
    })
  },
  toDetail (e) {
    let index = e.currentTarget.dataset.index
    let id = this.data.list[index].id
    if (getApp().globalData.isVip !== 'Y' && !getApp().globalData.isLookVip) {
      wx.showToast({
        title: '这是会员专享商品！',
        icon: 'none'
      })
    } else {
      wx.navigateTo({
        url: '/pages/productDetail/productDetail?id=' + id + '&type=1',
      })
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.getList()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})